package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.dao.RedPackageDao;
import com.baichang.taoshunda.entity.Cash;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository
public class RedPackageDaoImpl extends BaseDaoImpl<Cash> implements RedPackageDao {



    /**
     * 用户邀请商家获取红包记录  redPackageQuery
     */
    @Override
    public Page<Map<String, Object>> redPackageQuery(int nowPage, int pageSize, String bussName, String userName, int status) {
        StringBuffer stringBuffer = new StringBuffer();
        List<Object> param = new ArrayList<Object>();
            stringBuffer.append("SELECT a.*,tb.buss_name AS bName,u.nick_name as uName,(select all_money from t_order where order_number = a.order_number) as order_all_money FROM t_rebate_amount a LEFT JOIN t_bussiness tb ON tb.id=a.company_id left join t_user u on a.user_id=u.id WHERE a.company_id IS NOT NULL and status <> -1 ");
        if (StringUtils.isNotBlank(bussName)) {
            stringBuffer.append(" and tb.buss_name like ?");
            param.add("%" + bussName + "%");
        }
        if (StringUtils.isNotBlank(userName)) {
            stringBuffer.append(" and u.nick_name like ?");
            param.add("%" + userName + "%");
        }
        if (status>0) {
            stringBuffer.append(" and a.status =  ?");
            param.add(status);
        }
        stringBuffer.append(" ORDER BY a.created DESC");
        System.out.println(stringBuffer.toString());
        return pageForMapList(stringBuffer.toString(), nowPage, pageSize, param.toArray());
    }

    @Override
    public boolean deleteRedPackage(int id) {
        String sql = "update t_rebate_amount a set a.status=?  where a.id=? ";
        return update(sql, -1, id) > 0;
    }


}
